package top.eggcode.plugins.query.clause;

import org.apache.commons.lang3.StringUtils;
import top.eggcode.plugins.query.def.LiteralSentence;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Title:
 * Description: TODO
 * Date: 2021/9/2 13:27
 *
 * @author JiaQi Ding
 * @version 1.0
 */
public class OrderByClause implements LiteralSentence {

    private final List<String> columns;

    public OrderByClause(String... columns) {
        this.columns = new LinkedList<>();
        Collections.addAll(this.columns, columns);
    }

    public OrderByClause(List<String> columns) {
        this.columns = columns;
    }

    public OrderByClause(String columnObject) {
        this.columns = new LinkedList<>();
        String[] columns = columnObject.split(",");
        for (String columnName : columns) {
            this.columns.add(columnName.trim());
        }
    }


    @Override
    public String render() {

        StringBuilder orderClause = new StringBuilder();
        orderClause.append("ORDER BY ");
        orderClause.append(StringUtils.join(columns, ", "));
        return orderClause.toString();
    }
}
